package cn.tedu.weibo.controller;

import cn.tedu.weibo.mapper.CommentMapper;
import cn.tedu.weibo.pojo.dto.CommentDTO;
import cn.tedu.weibo.pojo.entity.Comment;
import cn.tedu.weibo.pojo.vo.CommentVO;
import cn.tedu.weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comment/")
@Api(tags = "03.评论模块")
public class CommentController {

    @Autowired(required = false)
    private CommentMapper commentMapper;

    /**发布评论功能*/
    @PostMapping("insert")
    @ApiOperation(value = "发布评论功能")
    public int insertComment(@RequestBody CommentDTO commentDTO, HttpSession session){
        /**
         1.校验登录状态
         2.发布评论
         */
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            // 未登录
            return 2;
        }

        // 存入数据表:实体类Comment
        Comment comment = new Comment();
        // 复制 + 赋值
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setCreated(new Date());
        comment.setUserId(userVO.getId());
        // 调用接口方法
        commentMapper.insertComment(comment);
        return 1;
    }

    /**获取指定微博评论功能*/
    @GetMapping("selectByWeiboId")
    @ApiOperation(value = "获取评论功能")
    public List<CommentVO> selectComment(int id){
        return commentMapper.selectCount(id);
    }
}










